<?php
namespace app\controllers;
/**
 * Created by PhpStorm.
 * @author LiShaoWei
 * @param Date : 2016/4/8
 * @param Time : 11:16
 * @since 1.0
 */
use app\models\ContactForm;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPConnection;
use yii\filters\AccessControl;
use app\models\LoginForm;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\console\Exception;
use yii\web\NotFoundHttpException;
class HomeController extends Controller
{

//    public function behaviors()
//    {
//        return [
//            'access' => [
//                'class' => AccessControl::className(),
//                'only' => ['logout', 'signup','login'],
//                'rules' => [
//                    [
//                        'actions' => ['signup'],
//                        'allow' => true,
//                        'roles' => ['?'],
//                    ],
//                    [
//                        'actions' => ['logout'],
//                        'allow' => true,
//                        'roles' => ['@'],
//                    ],
//                ],
//            ],
//            'verbs' => [
//                'class' => VerbFilter::className(),
//                'actions' => [
//                    'logout' => ['post'],
//                ],
//            ],
//        ];
//    }
    public $layout='home';
    public function actionFun()
    {
        $address='127.0.0.1';
        $port=10008;
        $sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);//创建端口
        socket_bind($sock,$address,$port);//绑定端口
        socket_listen($sock,5);//监听
        do {
            if (($msgsock = socket_accept($sock)) === false) {
                echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n";
                break;
            }
            //welcome  发送到客户端
            $msg = "<font color='red'>".\Yii::$app->request->get('name')."</font><br/>";
            socket_write($msgsock, $msg, strlen($msg));
            echo 'read client message\n';
            $buf = socket_read($msgsock, 8192);
            $talkback = "received message:$buf\n";
            echo $talkback;
            if (false === socket_write($msgsock, $talkback, strlen($talkback))) {
                echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n";
            } else {
                echo 'send success';
            }
            socket_close($msgsock);
        }while(true);

        socket_close($sock);
    }
    //首页
    public function actionIndex()
    {
        error_reporting(E_ALL);
        set_time_limit(0);
//ob_implicit_flush();

        $address = '127.0.0.1';
        $port = 10009;
//创建端口
        if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
            echo "socket_create() failed :reason:" . socket_strerror(socket_last_error()) . "\n";
        }

//绑定
        if (socket_bind($sock, $address, $port) === false) {
            echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";
        }

//监听
        if (socket_listen($sock, 5) === false) {
            echo "socket_bind() failed :reason:" . socket_strerror(socket_last_error($sock)) . "\n";
        }

        do {
            //得到一个链接
            if (($msgsock = socket_accept($sock)) === false) {
                echo "socket_accepty() failed :reason:".socket_strerror(socket_last_error($sock)) . "\n";
                break;
            }
            //welcome  发送到客户端
            $msg = "<font color='red'>".\Yii::$app->request->get('name')."</font><br/>";
            socket_write($msgsock, $msg, strlen($msg));
            echo 'read client message\n';
            $buf = socket_read($msgsock, 8192);
            $talkback = "received message:$buf\n";
            echo $talkback;
            if (false === socket_write($msgsock, $talkback, strlen($talkback))) {
                echo "socket_write() failed reason:" . socket_strerror(socket_last_error($sock)) ."\n";
            } else {
                echo 'send success';
            }
            socket_close($msgsock);
        } while(true);
//关闭socket
        socket_close($sock);

    }
    //注册
    public function actionLogin()
    {
//error_reporting(E_ALL);
echo "<h2>tcp/ip connection </h2>\n";
$service_port = 10009;
$address = '127.0.0.1';

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$result = socket_connect($socket, $address, $service_port);
//要发送到服务端的信息。
 $send_data = "This data will Send to server!";

        while ($buffer = @socket_read($socket, 8192, PHP_NORMAL_READ)) {
            if (preg_match("/not connect/",$buffer)) {
                echo "don`t connect\n";
                break;
            } else {
                //服务端传来的信息
                echo "Buffer Data: " . $buffer . "\n";
                echo "Writing to Socket\n";
                // 将客户的信息写到通道中，传给服务器端
                if (!socket_write($socket, "$send_data\n")) {
                    echo "Write failed\n";
                }
                //服务器端收到信息后，客户端接收服务端传给客户端的回应信息。
                while ($buffer = socket_read($socket, 8192, PHP_NORMAL_READ)) {
                    echo "sent to server:$send_data\n response from server was:" . $buffer . "\n";
                }

            }
        }
//        $in = "HEAD / http/1.1\r\n";
//$in .= "HOST: localhost \r\n";
//$in .= "Connection: close\r\n\r\n";
//$out = "";
//echo "sending http head request ...";
//socket_write($socket, $in, strlen($in));
//echo  "OK\n";
//
//echo "Reading response:\n\n";
//while ($out = socket_read($socket, 8192)) {
//    echo $out;
//}
//echo "closeing socket..";
//socket_close($socket);
//echo "ok .\n\n";

    }
    public function actionRegister()
    {
        return $this->render('register');
    }
    public function actionContent()
    {
        $ContactFormModel= new ContactForm();
        return $this->render('content',['model'=>$ContactFormModel]);
    }
}